package com.soft.javase.day09.map.bookmanagementsystem;

import java.util.*;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/30
 */
public class BookManagementSystem {

    private static Map<String, List<Book>> books = new HashMap<>();
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        List<Book> classic = new ArrayList<>();
        classic.add(new Book("名著", "西游记", 19.0));
        classic.add(new Book("名著", "水浒传", 29.0));
        books.put("名著", classic);

        List<Book> it = new ArrayList<>();
        it.add(new Book("It书籍", "Java入门", 99.0));
        it.add(new Book("It书籍", "PHP入门", 9.9));
        books.put("It书籍", it);

        while (true) {
            showMenu();
            int choice = sc.nextInt();

            switch (choice) {
                case 1:
                    showAllBooks();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    modifyBook();
                    break;
                case 5:
                    System.out.println("谢谢你的使用");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }

    private static void showMenu() {
        System.out.println("-----------欢迎来到图书管理系统-----------");
        System.out.println("1.查看所有书籍");
        System.out.println("2.添加书");
        System.out.println("3.删除书");
        System.out.println("4.修改书");
        System.out.println("5.退出");
        System.out.println("请输入你的选择：");
    }

    private static void showAllBooks() {
        System.out.println("类型        书名          价格");
        for (Map.Entry<String, List<Book>> entry : books.entrySet()) {
            String currentType = entry.getKey();
            System.out.println(currentType);

            for (Book book : entry.getValue()) {
                System.out.println("\t\t\t" + book.getName() + "\t\t" + book.getPrice());
            }
        }
    }

    private static void addBook() {
        System.out.println("请输入要添加书籍的类型：");
        String type = sc.next();

        System.out.println("请输入要添加的书名：");
        String name = sc.next();

        System.out.println("请输入要添加书的价格：");
        double price = sc.nextDouble();

        // 如果该类型不存在，则创建新的类型
        if (!books.containsKey(type)) {
            books.put(type, new ArrayList<>());
        }else {
            List<Book> bookList = books.get(type);
            if (bookList.contains(new Book(type, name, price))){
                System.out.println("已有此书");
            }else {
                bookList.add(new Book(type, name, price));
                System.out.println("添加" + name + "成功");
            }
        }
    }

    private static void deleteBook() {
        System.out.println("请输入要删除书籍的类型：");
        String type = sc.next();

        if (!books.containsKey(type)) {
            System.out.println("该类型不存在");
            return;
        }

        System.out.println("请输入要删除的书名：");
        String name = sc.next();

        List<Book> bookList = books.get(type);
        boolean removed = false;

        for (int i = 0; i < bookList.size(); i++) {
            Book book = bookList.get(i);
            if (book.getName().equals(name)) {
                bookList.remove(i);
                removed = true;
                break;
            }
        }

        if (removed) {
            System.out.println("删除" + name + "书籍成功");

            // 如果该类型下没有书了，删除该类型
            if (bookList.isEmpty()) {
                books.remove(type);
            }
        } else {
            System.out.println("该书不存在");
        }
    }

    private static void modifyBook() {
        System.out.println("请输入要修改书籍的类型：");
        String type = sc.next();

        if (!books.containsKey(type)) {
            System.out.println("该类型不存在");
            return;
        }

        System.out.println("请输入要修改的书名：");
        String oldName = sc.next();

        List<Book> bookList = books.get(type);
        Book foundBook = null;

        for (Book book : bookList) {
            if (book.getName().equals(oldName)) {
                foundBook = book;
                break;
            }
        }

        if (foundBook == null) {
            System.out.println("该书不存在");
            return;
        }

        System.out.println("请输入新的书名：");
        String newName = sc.next();

        System.out.println("请输入新的价格：");
        double newPrice = sc.nextDouble();

        foundBook.setName(newName);
        foundBook.setPrice(newPrice);

        System.out.println("修改成功");
    }
}
